package  
{
	import org.flixel.*;
	import org.flixel.plugin.photonstorm.*;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	/**
	 * ...
	 * @author le-ray_y
	 */
	public class CustomButton extends FlxExtendedSprite 
	{
		// Cooldown
		private var _cooldown:Number;
		private var _countdown:Number = 0;
		
		// Timer
		private var _timer:Timer;
		
		// Cooldown Text
		public var _cooldownText:FlxText;
		
		public function CustomButton(xPos:Number, yPos:Number, cooldown:Number, callback:Function) 
		{
			super(xPos, yPos);
			
			this.enableMouseClicks(false);
			
			_cooldown = cooldown;
			
			this.mousePressedCallback = callback;
			
			// Cooldown Text
			_cooldownText = new FlxText(x, y + 70, 60, "0");
			_cooldownText.alignment = "center";
			_cooldownText.size = 20;
		}
		
		public function startCooldown():void
		{
			FlxG.state.add(_cooldownText);
			_cooldownText.text = String(_cooldown)
			_timer = new Timer(1000, 0);
			_timer.start();
			_timer.addEventListener(TimerEvent.TIMER, coolDownHandler);
		}
		
		private function coolDownHandler(e:TimerEvent):void 
		{
			_countdown = _cooldown - _timer.currentCount;
			_cooldownText.text = String(_countdown);
			
			if (_countdown <= 0)
			{
				FlxG.state.remove(_cooldownText);
				_timer.stop();
				_timer.removeEventListener(TimerEvent.TIMER, coolDownHandler);
				this.exists = true;
			}
			
		}
		
	}

}